package com.topboya.linker.ui.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.topboya.linker.R;
import com.topboya.linker.diyview.group.GroupItemDecoration;
import com.topboya.linker.diyview.group.GroupRecyclerView;
import com.topboya.linker.model.levelitem.SelectDeviceGroup;
import com.topboya.linker.model.response.CustomerByOrgIdAndNewlyCusListResopnse;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.DictByKeyResponse;
import com.topboya.linker.model.response.NewlyOrgListResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.adapter.CreateTaskAdapter;
import com.topboya.linker.ui.adapter.SelectDevicesAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.CustomerPresenter;
import com.topboya.linker.ui.view.CustomerView;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SelectDeviceActivity extends BaseActivity<CustomerView, CustomerPresenter> implements CustomerView {

    private static final String TAG = "CustomerActivity-";
    private Toolbar toolbar;
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private SmartRefreshLayout smartrefreshlayout;
    private GroupRecyclerView grecycle_create_devices;
    private EditText et_search_devices;
    private int pageNO;
    private ArrayList<OrganizationResopnse.DocDetailInfoList> groupList;
    private ArrayList<NewlyOrgListResponse.Data> newLyData;
    private ArrayList<SelectDeviceGroup> selectDeviceGroups;
    private CreateTaskAdapter createTaskAdapter;
    private SelectDevicesAdapter selectDevicesAdapter;

    @Override
    protected int getLayoutId() {

        return R.layout.activity_select_customer;
    }

    @Override
    protected void initView() {

        toolbar = findViewById(R.id.toolbar);
        toolbar_title = findViewById(R.id.toolbar_title);
        et_search_devices = findViewById(R.id.et_search_devices);
        grecycle_create_devices = findViewById(R.id.grecycle_create_devices);
        smartrefreshlayout = findViewById(R.id.smart_refreshlayout);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        smartrefreshlayout.setEnableRefresh(false);//禁止刷新
        toolbar_title.setText("选择终端");
        tv_toolbar_right.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {

        groupList = new ArrayList<>();
        newLyData = new ArrayList<>();
        selectDeviceGroups = new ArrayList<>();
        pageNO = 1;
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        grecycle_create_devices.setLayoutManager(layoutManager);
//        createTaskAdapter = new CreateTaskAdapter(this,this.groupList);
        grecycle_create_devices.addItemDecoration(new GroupItemDecoration<String, String>());
//        grecycle_create_devices.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        selectDevicesAdapter = new SelectDevicesAdapter(this,groupList,newLyData);
        grecycle_create_devices.setAdapter(selectDevicesAdapter);
        grecycle_create_devices.notifyDataSetChanged();

        mPresenter.getOrgList(String.valueOf(pageNO), null);


    }

    @Override
    protected void initListener() {
        toolbar.setNavigationOnClickListener(v -> {
            finish();
        });
        smartrefreshlayout.setOnLoadmoreListener(refreshlayout -> {
            pageNO++;
            mPresenter.getOrgList(String.valueOf(pageNO), null);

        });
        et_search_devices.setOnEditorActionListener((v, actionId, event) -> {

             hideKeyboard(et_search_devices);
            mPresenter.getOrgList(String.valueOf(0), v.getText().toString().trim());
            return false;

        });
    }
    public static void hideKeyboard(View view) {

        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    @Override
    protected CustomerPresenter createPresenter() {

        return new CustomerPresenter(this);
    }


    @Override
    public void setCustomeData(ArrayList<OrganizationResopnse.DocDetailInfoList> groupList,ArrayList<NewlyOrgListResponse.Data> groupHListNewly) {

        LogUtils.v(TAG + "groupListNewly:" + groupHListNewly.toString());
        this.groupList.addAll(groupList);
        //业务需求目前需要clear一下，只展示最近10条的数据
        this.newLyData.clear();
        this.newLyData.addAll(groupHListNewly);
        LogUtils.v(TAG + "groupList:size()" + groupHListNewly.size());
//        selectDevicesAdapter.notifyDataSetChanged();
        for (int i=0;i < newLyData.size();i++){
            SelectDeviceGroup selectDeviceGroup = new SelectDeviceGroup();
            selectDeviceGroup.setId(newLyData.get(i).getId());
            selectDeviceGroup.setOrgName(newLyData.get(i).getOrgName());
            selectDeviceGroups.add(selectDeviceGroup);
        }
        for (int i=0;i < groupList.size();i++){
            SelectDeviceGroup selectDeviceGroup = new SelectDeviceGroup();
            selectDeviceGroup.setId(groupList.get(i).getId());
            selectDeviceGroup.setOrgName(groupList.get(i).getOrgName());
            selectDeviceGroups.add(selectDeviceGroup);
        }
         selectDevicesAdapter.resetGroups();
//        将最近和所有的数据拼接起来，点击事件好取到对应的数据
        selectDevicesAdapter.setItemData(selectDeviceGroups);
        grecycle_create_devices.notifyDataSetChanged();
        smartrefreshlayout.finishLoadmore();

    }



    @Override
    public void setDepartment(List<CustomerByOrgIdResponse.DepartmentList> departmentlist) {

        LogUtils.v(TAG + "setDepartment()");
//        customerAdapter.refreshItem(groupList,departmentgroupmap,postion);
    }

    @Override
    public void setDictByKey(List<DictByKeyResponse.Data> data) {

    }

    @Override
    public void createCustomer() {



    }

    @Override
    public void setDepartVisitor(HashMap<String, List<CustomerByOrgIdAndNewlyCusListResopnse.CusListChild>> departmentgroupmap, List<CustomerByOrgIdAndNewlyCusListResopnse.NewlyCusList> newlyCusLists) {

    }


}
